﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace Evolution.UI.WebParts
{
    [Serializable]
    public class ServerVariableAssertion : BaseKeyValueAssertion
    {
        #region Properties

        public override string Caption
        {
            get { return "Server variable"; }
        }

        #endregion

        #region Methods

        public override void CreateInterface(ControlCollectionBuilder builder, PageActionEditor editor, int index)
        {
            // Key
            TextBox key = new TextBox();
            key.ID = "SVKey" + index;
            key.CssClass = "UserInput";
            key.Style[HtmlTextWriterStyle.Width] = "175px";
            key.ToolTip = "Collection key";

            builder.AddControl(key);

            builder.AddStaticText(@"&nbsp;<input class=""ms-PropGridBuilderButton"" id=""INPUTIDSelectServerVariable{0}"" title=""Click to select the server variable"" style=""DISPLAY: inline"" onclick=""javascript:showModalDialog('{1}',{2},'dialogHeight:540px;dialogWidth:450px;help:no;status:no;resizable:no');"" type=""button"" value=""..."" /></NOBR>",
                                  index, SPContext.Current.Site.Url + "/_layouts/Evolution/code/ServerVariablesDialogBox.htm", key.ClientID);

            builder.AddErrorPlaceholder("SVCollectionsKey" + index);

            builder.AddStaticText("<br/><br/>");

            DropDownList list1 = new DropDownList();
            list1.ID = "SVOperator" + index;
            list1.Items.Add(new ListItem("is equal to", KeyValueOperators.IsEqualTo.ToString()));
            list1.Items.Add(new ListItem("is not equal to", KeyValueOperators.IsNotEqualTo.ToString()));
            list1.Items.Add(new ListItem("is greater than", KeyValueOperators.IsGreaterThan.ToString()));
            list1.Items.Add(new ListItem("is less than", KeyValueOperators.IsLessThan.ToString()));
            list1.Items.Add(new ListItem("is greater than or equal to", KeyValueOperators.IsGreaterThanOrEqualTo.ToString()));
            list1.Items.Add(new ListItem("is less than or equal to", KeyValueOperators.IsLessThanOrEqualTo.ToString()));
            list1.Items.Add(new ListItem("begins with", KeyValueOperators.BeginsWith.ToString()));
            list1.Items.Add(new ListItem("does not begin with", KeyValueOperators.DoesNotBeginWith.ToString()));
            list1.Items.Add(new ListItem("ends with", KeyValueOperators.EndsWith.ToString()));
            list1.Items.Add(new ListItem("does not end with", KeyValueOperators.DoesNotEndWith.ToString()));
            list1.Items.Add(new ListItem("contains", KeyValueOperators.Contains.ToString()));
            list1.Items.Add(new ListItem("does not contain", KeyValueOperators.DoesNotContain.ToString()));
            list1.Items.Add(new ListItem("match", KeyValueOperators.Match.ToString()));
            list1.Items.Add(new ListItem("does not match", KeyValueOperators.DoesNotMatch.ToString()));
            list1.Style[HtmlTextWriterStyle.Width] = "175px";

            builder.AddControl(list1);

            builder.AddStaticText("<br/><br/>");

            // Value
            TextBox value = new TextBox();
            value.ID = "SVValue" + index;
            value.CssClass = "UserInput";
            value.Style[HtmlTextWriterStyle.Width] = "175px";
            value.ToolTip = "Value to compare";

            builder.AddControl(value);
        }

        public override string GetDescription()
        {
            return this.InternalGetDescription("Server variable");
        }


        public override void SyncChanges(PageActionEditor editor, int index)
        {
            this.InternalSyncChanges("SV", editor, index);
        }

        public override void ApplyChanges(PageActionEditor editor, int index)
        {
            this.InternalApplyChanges("SV", editor, index);
        }

        public override bool Evaluate(Page page, PageActionWebPart part)
        {
            string value = page.Request.ServerVariables[this.Key];
            if (value == null)
                value = String.Empty;

            return this.InternalEvaluate(value, page, part);
        }

        #endregion
    }
}
